from threading import Thread

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_jwt_extended import JWTManager
#from flask_login import LoginManager
from flask_socketio import SocketIO
from flask_redis import FlaskRedis
from flask_marshmallow import Marshmallow # 自动化 序列化利用db创建的类表


db = SQLAlchemy()
migrate = Migrate()
#login_manager = LoginManager()
jwt = JWTManager()
socketio = SocketIO()
redis_client = FlaskRedis(decode_responses=True) # decode_responses参数：默认为False，返回字节，True返回字符串
ma = Marshmallow()

def create_app():
    app = Flask(__name__)
    app.config.from_object('config')
    # 注册
    db.init_app(app)
    ma.init_app(app)
    jwt.init_app(app)
    migrate.init_app(app, db)
    #login_manager.init_app(app)
    socketio.init_app(app)
    redis_client.init_app(app)


    # 注册蓝图
    from app.auth import auth_bp
    from app.web import web_bp
    from app.apis import api_bp
    app.register_blueprint(auth_bp)
    app.register_blueprint(web_bp)
    app.register_blueprint(api_bp)
    # 启动线程，监控不活跃用户
    print(app.url_map)

    return app